package org.bovver.restful;

import org.bovver.model.User;
import org.bovver.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;
import java.util.concurrent.atomic.AtomicLong;

/**
 * Created by Paul on 2014/11/21.
 *
 */
@RestController
@RequestMapping(value = "/users", consumes = "application/json", headers = "version=1.0")
public class UserController {

    private UserService userService;

    @Autowired
    public void setUserService(UserService userService) {
        this.userService = userService;
    }

    @RequestMapping(value = "/{id}", method = RequestMethod.GET)
    public Object getUserById(@PathVariable("id") Integer id) {
        User user = new User();
        user.setId(id);
        user.setPassword("12332");
        user.setUsername("bovver");
        return user;
    }

    @RequestMapping(value = "/{id}", method = RequestMethod.POST)
    public User update(User user, @PathVariable("id") String id) {
        User user1 = new User();
        user1.setId(1);
        user1.setPassword("12332");
        user1.setUsername("bovver");

        return user;
    }

    @RequestMapping
    public List<User> index() {
        return userService.getUsers();
    }

    @RequestMapping(value = "/pagination/index{index}_size{size}")
    public List<User> page(@PathVariable("index") Integer index, @PathVariable("size") Integer size) {
        User user = new User();
        user.setId(1);
        user.setPassword("12332");
        user.setUsername("bovver" + index + "_" + size);
        List<User> users = new ArrayList<>();
        users.add(user);
        return users;
    }

    @RequestMapping(value = "/more/last{last}_size{size}")
    public List<User> more(@PathVariable("last") Integer last, @PathVariable("size") Integer size) {
        int lastId = last == null || last == 0 ? Integer.MAX_VALUE : last;
        return userService.getMoreUsers(lastId, size);
    }

    @RequestMapping(value = "/users/[username:{username},password:{password}]")
    public List<User> getUsers(@PathVariable("username") String username, @PathVariable("password") String password) {
        User user1 = new User();
       // user1.setId(user.getId());
        user1.setPassword(password);
        user1.setUsername(username);
        List<User> users = new ArrayList<>();
        users.add(user1);
        return users;
    }

    @RequestMapping("/product")
    public Map<String, Object> getProduct() {
        Map<String, Object> pro = new LinkedHashMap<>();
        pro.put("name", "jalkdj");
        pro.put("price", "100");
        return pro;
    }

}
